package com.itheima.service.Impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;


import javax.sql.DataSource;
import java.util.List;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void save(Account account) {
        accountDao.save(account);
    }

    public void delete(Integer id) {
        accountDao.delete(id);
    }

    public void update(Account account) {
        accountDao.update(account);
    }

    public List<Account> findAll() {

        return accountDao.findAll();
    }

    public Account findById(Integer id) {

        return accountDao.findById(id);
    }


    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
@Transactional
    public void transfer(String outName, String inName, Double money) {
        //开启事务（事物管理器）
        DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
        //
        TransactionStatus status = manager.getTransaction(null);
        accountDao.outMoney(outName, money);
   //  int i=1/0;
        accountDao.inMoney(inName, money);
        manager.commit(status);
    }
}
